/*****************************************************************************
	SNEGA UTILITIES 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/snega-utilities/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package org.sng.util.db.elmt;

import java.sql.*;
import java.util.*;

import org.sng.util.db.*;
import org.sng.util.db.rawsql.*;
import org.sng.util.var.*;
import org.sng.util.web.elmt.*;

public class DataTable extends SObject {
	
	protected GeneralOpForDatabase m_dbo;
	protected SQLSelect m_sql;
	
	protected DataRow [] m_rows;
	
	protected int m_rowCount;
	
	public DataTable()
	{
		this.m_dbo = null;
		this.m_sql = null;
		
		this.m_rows = null;
		
		this.m_rowCount = -1;
	}
	
	public DataTable(SQLSelect sql)
	{
		this.m_dbo = sql.GetDbo();
		this.m_sql = sql;
		
		this.m_rows = null;
		
		this.m_rowCount = -1;
	}
	
	public DataRow [] GetRows()
	{
		return this.m_rows;
	}
	
	public DataRow Rows(int n)
	{
		return this.m_rows[n];
	}
	
	public DataRow NextRow()
	{
		if (this.m_rows == null)
		{
			return null;
		}
		else
		{
			this.m_rowCount++;
			
			if (this.m_rowCount >= this.m_rows.length)
			{
				return null;
			}
			else {
				return this.m_rows[this.m_rowCount];
			}
		}
	}
	
	public void ResetCurrentRow()
	{
		this.m_rowCount = -1;
	}
	
	public DataRow Get(Object index)
	{
		return null;
	}
	
	protected void refresh()
	{
		if (this.m_sql == null)
		{
			return;
		}
		
		LinkedList<DataRow> ll = new LinkedList<DataRow>();
		
		DbConn dc = this.m_sql.GetDbo().executeQuery(this.m_sql.ToSQL());

		try {
			// Fetch meta data
			ResultSetMetaData md = dc.rs.getMetaData();
			int colNum = md.getColumnCount();
			
			// Add rows
			while (dc.rs.next())
			{
				DataRow row = new DataRow();
				
				for (int i = 0; i < colNum; i++)
				{
					row.PushCell(md.getColumnName(i + 1), dc.rs.getString(i + 1), md.getColumnType(i + 1));
				}
				//SLog.println(md.getColumnName(1) + "|" + dc.rs.getString(1));
				ll.add(row);
			}
			
			// Convert linked list to array
			this.m_rows = null;
			this.m_rows = new DataRow[ll.size()];
			ll.toArray(this.m_rows);
			
			// Push property
			this.PushVar("length", this.m_rows.length);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dc.close();
		}
	}
	
	public void Refresh()
	{
		this.refresh();
	}
	
	public void Submit()
	{
		if (this.m_dbo == null)
		{
			return;
		}
		
		try
		{
			
		} 
		catch (Exception e) 
		{
			
		}
	}
	
	public SHashMap ToSHashMapFromColumnIndex(String index)
	{
		SHashMap o = new SHashMap();
		
		for (int i = 0; i < this.m_rows.length; i++) {
			o.PushVar(this.m_rows[i].GetCellValueString(index), this.m_rows[i]);
		}
		
		return o;
	}
	
	// override methods
	public int GetLength()
	{
		return this.m_rows.length;
	}
	
	public SVoid GetMember(RuntimeEnvironment in, int n)
	{
		return this.Rows(n);
	}

}
